/**
 * 
 */
package com.acxiom.bsh.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.InvalidResultSetAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.support.rowset.SqlRowSet;
import org.springframework.stereotype.Repository;

import com.acxiom.bsh.dao.LuckyDrawDouble11Dao;
import com.acxiom.bsh.logger.LoggerManager;
import com.acxiom.bsh.pojo.Double11Prize;
import com.acxiom.bsh.pojo.Double11PrizeRecord;

/**
 * @Description: TODO
 * @author jerche
 *
 * @updateUser
 * @updateDate
 */
@Repository
public class LuckyDrawDouble11DaoImpl implements LuckyDrawDouble11Dao {

	@Override
	public int get1234PrizeDouble12Times(String mixNick, int prizeLevel) {
			try {
				String sql = "select count(1) as num from double11prizerecord a join double11prize b on a.prizeId=b.id where a.mixNick=? and a.prizeDateTime >'2017-12-05 22:00:00' and a.prizeDateTime <'2017-12-12 23:59:59' and b.prizeLevel =?";

				SqlRowSet set = jdbcTemplate.queryForRowSet(sql, mixNick,prizeLevel);
				if (set.next()) {
					return set.getInt("num");
				}
			} catch (InvalidResultSetAccessException e) {
				LoggerManager.getErrorLogger().error("getCurrentDay1234PrizeTimes Error:", e);
			} catch (DataAccessException e) {
				LoggerManager.getErrorLogger().error("getCurrentDay1234PrizeTimes  DataAccess Error:", e);
			}

			return 0;
	}

	@Autowired
	private JdbcTemplate jdbcTemplate;

	@Override
	public int getCurrentDayDrawTimes(String mixNick) {
		try {
			String sql = "select count(1) as num from double11prizerecord where mixNick=? and prizeDateTime >'2017-12-05 22:00:00' and prizeDateTime <'2017-12-12 23:59:59'";

			SqlRowSet set = jdbcTemplate.queryForRowSet(sql, mixNick);
			if (set.next()) {
				return set.getInt("num");
			}
		} catch (InvalidResultSetAccessException e) {
			e.printStackTrace();
			LoggerManager.getErrorLogger().error(" Double11 getCurrentDayDrawTimes Error:", e);
		} catch (DataAccessException e) {
			e.printStackTrace();
			LoggerManager.getErrorLogger().error(" Double11 getCurrentDayDrawTimes  DataAccess Error:", e);
		}
		return 0;
	}

	@Override
	public List<Double11Prize> getDouble11Prize() {
		String sql = "select id,giftName,giftDescription,prizeLevel,minTimes,probability,currentInventory,usedInventory,totalInventory," + "creationDateTime,updateDateTime,creationUID,updateUID,flag,giftType,pointCode,needPurchase,startTime,endTime  from double11prize where flag=1 order by prizeLevel asc";

		List<Double11Prize> prizeList = null;
		try {
			SqlRowSet set = jdbcTemplate.queryForRowSet(sql);

			prizeList = new ArrayList<Double11Prize>();
			while (set.next()) {
				Double11Prize prize = new Double11Prize();
				prize.setId(set.getInt("id"));
				prize.setGiftName(set.getString("giftName"));
				prize.setPrizeLevel(set.getInt("prizeLevel"));
				prize.setMinTimes(set.getInt("minTimes"));
				prize.setProbability(set.getDouble("probability"));
				prize.setCurrentInventory(set.getInt("currentInventory"));
				prize.setUsedInventory(set.getInt("usedInventory"));
				prize.setTotalInventory(set.getInt("totalInventory"));
				prize.setGiftType(set.getInt("giftType"));
				prize.setPointCode(set.getString("pointCode"));
				prize.setNeedPurchase(set.getInt("needPurchase"));
				prizeList.add(prize);
			}
		} catch (InvalidResultSetAccessException e) {
			e.printStackTrace();
			LoggerManager.getErrorLogger().error("Double11 getDouble11Prize  ResultSetAccess Error:", e);
		} catch (DataAccessException e) {
			e.printStackTrace();
			LoggerManager.getErrorLogger().error("Double11 getDouble11Prize  DataAccess Error:", e);
		}
		return prizeList;
	}

	@Override
	public int getTotalDrawTimes(String mixNick) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int updateInventory(Double11Prize prize) {
		int result = 0;
		try {
			String sql = "update double11prize set currentInventory=currentInventory-1,usedInventory=usedInventory+1,updateDateTime=now() where id=? and currentInventory > 0 and flag=1";
			result = jdbcTemplate.update(sql, prize.getId());
		} catch (DataAccessException e) {
			e.printStackTrace();
			LoggerManager.getErrorLogger().error("updateInventory  DataAccess Error:", e);
		}
		return result;
	}

	@Override
	public int saveDouble11PrizeRecord(Double11PrizeRecord record) {
		int result = 0;
		try {
			String sql = "INSERT INTO bsh_tmall.double11prizerecord(mixNick,prizeId,prizeName,prizeDateTime,sellerName,creationDateTime,updateDateTime,creationUID,updateUID)" + " VALUES(?,?,?,now(),?,now(),now(),'lucky_draw','lucky_draw')";
			result = jdbcTemplate.update(sql, record.getMixNick(), record.getPrizeId(), record.getPrizeName(),record.getSellerName());
		} catch (DataAccessException e) {
			e.printStackTrace();
			LoggerManager.getErrorLogger().error("saveDouble11Prizerecord  DataAccess Error:", e);
		}
		return result;
	}

	@Override
	public List<Double11PrizeRecord> getDouble11PrizeRecord(String mixNick) {
		String sql = "SELECT a.id,mixNick,prizeId,prizeName,prizeDateTime,prizeLevel FROM double11prizerecord a join double11prize b on a.prizeId=b.id where mixNick=? order by prizeDateTime desc";

		List<Double11PrizeRecord> prizeList = null;
		try {
			SqlRowSet set = jdbcTemplate.queryForRowSet(sql, mixNick);

			prizeList = new ArrayList<Double11PrizeRecord>();
			while (set.next()) {
				Double11PrizeRecord prize = new Double11PrizeRecord();
				prize.setId(set.getInt("id"));
				prize.setMixNick(set.getString("mixNick"));
				prize.setPrizeId(set.getInt("prizeId"));
				prize.setPrizeLevel(set.getInt("prizeLevel"));
				prize.setPrizeName(set.getString("prizeName"));
				prize.setPrizeDateTime(set.getDate("prizeDateTime"));

				prizeList.add(prize);
			}
		} catch (InvalidResultSetAccessException e) {
			e.printStackTrace();
			LoggerManager.getErrorLogger().error("getPrizeRecord  ResultSet Error:", e);
		} catch (DataAccessException e) {
			e.printStackTrace();
			LoggerManager.getErrorLogger().error("getPrizeRecord  DataAccess Error:", e);
		}
		return prizeList;
	}

	@Override
	public Double11Prize getDefaultDouble11Prize() {
		String sql = "select id,giftName,giftDescription,prizeLevel,minTimes,probability,currentInventory,usedInventory,totalInventory," + "creationDateTime,updateDateTime,creationUID,updateUID,flag,giftType,pointCode,needPurchase from double11prize where flag=1 and defaultFlag = 1";

		Double11Prize prize = null;
		try {
			SqlRowSet set = jdbcTemplate.queryForRowSet(sql);

			prize = new Double11Prize();
			if (set.next()) {
				prize.setId(set.getInt("id"));
				prize.setGiftName(set.getString("giftName"));
				prize.setPrizeLevel(set.getInt("prizeLevel"));
				prize.setMinTimes(set.getInt("minTimes"));
				prize.setProbability(set.getDouble("probability"));
				prize.setCurrentInventory(set.getInt("currentInventory"));
				prize.setUsedInventory(set.getInt("usedInventory"));
				prize.setTotalInventory(set.getInt("totalInventory"));
				prize.setGiftType(set.getInt("giftType"));
				prize.setPointCode(set.getString("pointCode"));
				prize.setNeedPurchase(set.getInt("needPurchase"));
			}
		} catch (InvalidResultSetAccessException e) {
			e.printStackTrace();
			LoggerManager.getErrorLogger().error("getDefaultPrize  ResultSet Error:", e);
		} catch (DataAccessException e) {
			e.printStackTrace();
			LoggerManager.getErrorLogger().error("getDefaultPrize  DataAccess Error:", e);
		}
		return prize;
	}

	@Override
	public List<Double11PrizeRecord> getDouble11PrizeRecordTop5() {
		String sql = "SELECT a.id,mixNick,prizeId,prizeName,prizeDateTime,prizeLevel FROM double11prizerecord a  join  double11prize b  on a.prizeId =b.id  where  b.prizeLevel in (1,2,3)   order by prizeLevel asc LIMIT  0,5";

		List<Double11PrizeRecord> prizeList = null;
		try {
			SqlRowSet set = jdbcTemplate.queryForRowSet(sql);

			prizeList = new ArrayList<Double11PrizeRecord>();
			while (set.next()) {
				Double11PrizeRecord prize = new Double11PrizeRecord();
				prize.setId(set.getInt("id"));
				prize.setMixNick(set.getString("mixNick"));
				prize.setPrizeId(set.getInt("prizeId"));
				prize.setPrizeLevel(set.getInt("prizeLevel"));
				prize.setPrizeName(set.getString("prizeName"));
				prize.setPrizeDateTime(set.getDate("prizeDateTime"));

				prizeList.add(prize);
			}
		} catch (InvalidResultSetAccessException e) {
			e.printStackTrace();
			LoggerManager.getErrorLogger().error("getDouble11PrizeRecordTop5  ResultSet Error:", e);
		} catch (DataAccessException e) {
			e.printStackTrace();
			LoggerManager.getErrorLogger().error("getDouble11PrizeRecordTop5  DataAccess Error:", e);
		}
		return prizeList;
	}

}
